/**
 * Copyright (C) 2009 - 2013 SC 4ViewSoft SRL
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.achartengine.chartdemo.demo.slice;

import org.achartengine.chartdemo.demo.ResourceTable;
import org.achartengine.chartdemo.demo.SampleItem;
import org.achartengine.chartdemo.demo.SampleItemProvider;
import org.achartengine.chartdemo.demo.chart.*;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;

import java.util.ArrayList;
import java.util.List;


public class ChartDemo extends AbilitySlice {
    private IDemoChart[] mCharts = new IDemoChart[]{new AverageTemperatureChart(),
            new AverageCubicTemperatureChart(), new SalesStackedBarChart(), new SalesBarChart(),
            new TrigonometricFunctionsChart(), new ScatterChart(), new SalesComparisonChart(),
            new ProjectStatusChart(), new SalesGrowthChart(), new BudgetPieChart(),
            new BudgetDoughnutChart(), new ProjectStatusBubbleChart(), new TemperatureChart(),
            new WeightDialChart(), new SensorValuesChart(), new CombinedTemperatureChart(),
            new MultipleTemperatureChart()};

    private String[] mMenuText;

    private String[] mMenuSummary;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);
        int length = mCharts.length;
        mMenuText = new String[length + 3];
        mMenuSummary = new String[length + 3];
        mMenuText[0] = "Embedded line chart demo";
        mMenuSummary[0] = "A demo on how to include a clickable line chart into a graphical activity";
        mMenuText[1] = "Embedded pie chart demo";
        mMenuSummary[1] = "A demo on how to include a clickable pie chart into a graphical activity";
        for (int i = 0; i < length; i++) {
            mMenuText[i + 2] = mCharts[i].getName();
            mMenuSummary[i + 2] = mCharts[i].getDesc();
        }
        mMenuText[length + 2] = "Random values charts";
        mMenuSummary[length + 2] = "Chart demos using randomly generated values";

    }

    @Override
    protected void onActive() {
        super.onActive();
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_list_container);
        SampleItemProvider sampleItemProvider = new SampleItemProvider(getListValues(), this);
        listContainer.setItemProvider(sampleItemProvider);
        listContainer.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                Intent intent = new Intent();
                Operation operation=null;
                // 指定待启动FA的bundleName和abilityName
                if (i == 0) {
//                    intent = new Intent(this, XYChartBuilder.class);
                    operation = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName(getBundleName())
                            .withAbilityName("org.achartengine.chartdemo.demo.chart.XYCharBuilderAbility")
                            .build();
                } else if (i == 1) {
//                    intent = new Intent(this, PieChartBuilder.class);
                    operation = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName(getBundleName())
                            .withAbilityName("org.achartengine.chartdemo.demo.PieChartBuilderAbility")
                            .build();
                } else if (i <= mCharts.length + 1) {
                    intent = mCharts[i - 2].execute(ChartDemo.this);
//                    operation = new Intent.OperationBuilder()
//                            .withDeviceId("")
//                            .withBundleName("com.achartengine.demo")
//                            .withAbilityName("com.achartengine.demo.SecondAbility")
//                            .build();
//                    operation = new Intent.OperationBuilder()
//                            .withDeviceId("")
//                            .withBundleName("com.achartengine.demo")
//                            .withAbilityName("com.achartengine.demo.GraphicalActivity")
//                            .build();
                    startAbility(intent);
                    return;

                } else {
//                    intent = new Intent(this, GeneratedChartDemo.class);
                    operation = new Intent.OperationBuilder()
                            .withDeviceId("")
                            .withBundleName(getBundleName())
                            .withAbilityName("org.achartengine.chartdemo.demo.GeneratedChartDemoAbility")
                            .build();
                }

                intent.setOperation(operation);
                startAbility(intent);
            }

        });
    }

    /**
     * Called when the activity is first created.
     *
     * @return  List<SampleItem>
     */
    private List<SampleItem> getListValues() {
        List<SampleItem> values = new ArrayList<SampleItem>();
        int length = mMenuText.length;
        for (int i = 0; i < length; i++) {
            SampleItem v = new SampleItem();
            v.setName(mMenuText[i]);
            values.add(v);
        }
        return values;
    }



}